//
//  CS_Edit_Button.m
//  Pods
//
//  Created by 网新中研 on 2017/5/8.
//
//

#import "CS_Page_Button.h"

@interface CS_Page_Button()

@property (nonatomic, strong) UIButton *button;

@end

@implementation CS_Page_Button

-(NSInteger)viewType{
    return CSViewType_Page_Button;
}

-(instancetype)init{
    if (self = [super init]) {
        self.clickEnable = YES;
    }
    return self;
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    if (_button != nil) {
        [_button removeFromSuperview];
    }
    
    _button = [[UIButton alloc]init];
    [_button setTitle:self.buttonName forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    CGFloat leftMargin = self.leftMargin != 0 ? self.leftMargin : CELL_EDIT_RIGHT_MARGIN;
    CGFloat rightMargin = self.rightMargin != 0 ? self.rightMargin : CELL_EDIT_RIGHT_MARGIN;
    
    _button.frame = CGRectMake(leftMargin, 10, kWindowW - leftMargin - rightMargin, 40);
    _button.backgroundColor = COLOR_THEME;
    
    _button.titleLabel.font = [UIFont systemFontOfSize:16.0];

    [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_button];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, 60);
    _button.layer.cornerRadius = 5;
    
    if (self.isOtherStyle) {
        
        STYLE_SAVE_BUTTON(_button);
        
        _button.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
        _button.layer.borderColor = [UIColor colorFromHexCode:@"e4e4e4"].CGColor;
        _button.layer.borderWidth = 1.0;
        [_button setTitleColor:[UIColor colorFromHexCode:@"444444"] forState:UIControlStateNormal];
        
    }
    
    [superView addSubview:self];
    
    return primaryY + 60;
}

-(void)buttonClick{
    
    if (self.clickEnable) {
        if (self.clickHandler != nil) {
            self.clickHandler();
        }
    }
}

-(void)setButtonName:(NSString *)buttonName{
    
    if (buttonName == nil) {
        _buttonName = @"";
    }
    
    _buttonName = buttonName;
    
    if (self.button == nil) {
        return;
    }
    
    [self.button setTitle:_buttonName forState:UIControlStateNormal];
    
}

-(void)setIsOtherStyle:(BOOL)isOtherStyle{
    
    _isOtherStyle = isOtherStyle;
    
    if (isOtherStyle) {
        
        STYLE_SAVE_BUTTON(_button);
        self.button.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
        self.button.layer.borderColor = [UIColor colorFromHexCode:@"e4e4e4"].CGColor;
        self.button.layer.borderWidth = 1.0;
        [self.button setTitleColor:[UIColor colorFromHexCode:@"444444"] forState:UIControlStateNormal];
        
    }else{
        
        STYLE_SAVE_BUTTON(_button);
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.button.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, 10, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, 40);
        self.button.backgroundColor = COLOR_THEME;
        
    }

}

-(void)setClickEnable:(BOOL)clickEnable{
    
    _clickEnable = clickEnable;
    
    if (clickEnable) {
        STYLE_SAVE_BUTTON(_button);
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.button.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, 10, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, 40);
        self.button.backgroundColor = COLOR_THEME;
    }else{
        
        STYLE_SAVE_BUTTON(_button);
        self.button.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
        self.button.layer.borderColor = [UIColor colorFromHexCode:@"e4e4e4"].CGColor;
        self.button.layer.borderWidth = 1.0;
        [self.button setTitleColor:[UIColor colorFromHexCode:@"444444"] forState:UIControlStateNormal];
    }
    
}

@end
